<template>
  <div class="search-result-container">
    <!-- 点击实现后退效果 -->
    <van-nav-bar
      title="搜索结果"
      left-arrow
      @click-left="$router.go(-1)"
      fixed
    />
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <ArticleItem
        v-for="(item, index) in ArticleItemList"
        :key="index"
        :obj="item"
        @click.native="btn(item)"
        offset="40"
      ></ArticleItem>
    </van-list>
  </div>
</template>

<script>
import ArticleItem from '@/components/ArticleItem.vue'
import { searchResultAPI } from '@/api'
export default {
  name: 'SearchResult',
  data () {
    return {
      page: 1,
      ArticleItemList: [],
      loading: false,
      finished: false
    }
  },
  components: {
    ArticleItem
  },

  methods: {
    onLoad () {
      this.page++
      this.getResultList()
    },
    async getResultList () {
      const [, res] = await searchResultAPI({
        q: this.$route.params.keywords,
        page: this.page
      })
      console.log(res)
      this.ArticleItemList = [...this.ArticleItemList, ...res.data.data.results]
      this.loading = false
      if (res.data.data.results.length === 0) {
        this.finished = true
      }
    },
    btn (item) {
      console.log(item)
      this.$router.push({
        path: '/art_detail',
        query: {
          art_id: item.art_id
        }
      })
    }
  },
  created () {
    this.page = 1
    this.getResultList()
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
